#include "const.h"

void v_delete(var_t v) {
	size_t i;

	for(i=0; i<VAR_MAX_INHERITENCE; i++) {
		if (v->i.owners[i])
			v->i.owners[i]->i.owned = (var_t)0;
	}

	if (v->i.owned)
		v->i.owned->i.owners[v->i.owned_index] = (var_t)0;

	while(v->h.head)
		v_delete(v->h.head);

	v->flags &= ~VAR_CONST_BIT;

	if (~v->flags&VAR_SYMLINK_BIT) {
		if (v->value.text) {
			i_delete((void *)v->value.text);
			v->value.text = (char *)0;
		}

		if (v->expanded) {
			i_delete((void *)v->expanded);
			v->expanded = (char *)0;
		}

		v_array_empty(v);

		if (v->array.ptrvar)
			v_delete(v->array.ptrvar);
	}

	if (v->name)
		i_delete((void *)v->name);

	if (v->h.prev)
		v->h.prev->h.next = v->h.next;
	if (v->h.next)
		v->h.next->h.prev = v->h.prev;

	if (v->h.prnt->h.head)
		v->h.prnt->h.head = v->h.next;
	if (v->h.prnt->h.tail)
		v->h.prnt->h.tail = v->h.prev;

	i_delete((void *)v);
}
